<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../css/common.css" media="all">
	</head>
	<body>

		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						<!-- <div class="layui-card-header">网站设置</div> -->
						<div class="layui-card-body" pad15>
							<div class="layui-tab layui-tab-brief" lay-filter="changetab">
								<ul class="layui-tab-title" id="view_ul"></ul>
								<div class="layui-form margin_top10" lay-filter="test_detail">
									<div class="layui-form-item">
										<label class="layui-form-label">名称</label>
										<div class="layui-input-block">
											<input type="hidden" name="id" class="layui-input">
											<input type="text" name="name" class="layui-input">
										</div>
									</div>
									<div class="layui-form-item">
										<label class="layui-form-label">参数</label>
										<div class="layui-input-block">
											<input type="text" name="code" class="layui-input">
										</div>
									</div>
										
									<div class="layui-form-item">
										<div class="layui-input-block">
											<button class="layui-btn" lay-submit lay-filter="demo1">确认保存</button>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		
		<script type="text/html" id="demo_ul">
			{{#  layui.each(d.data, function(index, item){ }}
				<li class=" {{ index == 0 ? 'layui-this' : '' }}" lay-id='{{ item.id }}'>{{ item.name }}</li>
			{{# }); }}
		</script>
		
		<script src="../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../js/common.js" type="text/javascript" charset="utf-8"></script>
		<script>
			layui.config({
				base: '../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'laytpl', 'laydate','element'], function() {
				var $ = layui.$,
					admin = layui.admin,
					laytpl = layui.laytpl,
					element = layui.element,
					laydate = layui.laydate,
					form = layui.form;
				
				var _data = {},
					tabname = ''
				
				initdata();
				
				element.on('tab(changetab)', function(elem) {
					tabname = $(this).html();
					initdatadetail(this.getAttribute('lay-id'))
					form.render();
				});

				//监听提交
				form.on('submit(demo1)', function(data) {
					var data = data.field;
					_data = Object.assign(_data, data);

					console.log(_data);
					layer.confirm('确定修改数据？', function() {
						loading();
						submitdata();
					}, function() {
						layer.msg('取消操作');
					});
					return false;
				});

				function submitdata() {
					$.post(server.updatesite_config, _data, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								layer.alert('操作成功', function() {
									layer.closeAll();
								});
								initdatadetail(_data.id);
								if(tabname != _data.name){
									initdata();
								}
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

				function initdata() {
					$.get(server.querysite_config, {}, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								var data = res.site_config
								
								var getTpl = demo_ul.innerHTML,
									view = document.getElementById('view_ul');
								laytpl(getTpl).render(data, function(html) {
									view.innerHTML = html;
								});
								
								initdatadetail(data.data[0].id);
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
				function initdatadetail(id) {
					$.get(server.querysite_configdetail, {id: id}, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								var data = res.site_config
								
								form.val("test_detail", data.data);
								form.render();
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
			});
		</script>
	</body>
</html>